package aos.client.gui;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JSlider;

import org.apache.log4j.Logger;

import aos.client.robot.RemoteRobotControllerProxy;
import aos.server.robot.RobotControllerProxy;

import thinlet.Button;
import thinlet.CheckBox;
import thinlet.Menu;
import thinlet.MenuBar;
import thinlet.Pane;
import thinlet.RootPane;
import thinlet.ScrollPanel;
import thinlet.Slider;
import thinlet.Text;
import thinlet.Widget;
import thinlet.event.Listener;
import thinlet.layout.Constraint;
import thinlet.layout.InlineLayout;
import thinlet.layout.Layout;
import thinlet.layout.Metrics;
import thinlet.layout.RowLayout;
import thinlet.layout.TableLayout;


public class ControlPane extends Pane {

	static Logger logger = Logger.getLogger(ControlPane.class);
	
	HeadController head;
	WheelController wheel;
	RobotControllerProxy robotControllerProxy;
	AutoCheckBox status;
	
	public ControlPane(RobotControllerProxy robotControllerProxy) {
		
		setLayout(new RowLayout());
		
		this.robotControllerProxy = robotControllerProxy;
		
		add(new Block("Head"));
		head = new HeadController();
		add(head);
		
		add(new Block("Wheel"));
		wheel = new WheelController();
		add(wheel);
		
		status = new AutoCheckBox("Keyboard enabled");
		status.setSelected(false);
		add(status);
		
	}
	
	protected void process(AWTEvent e) {
		switch (e.getID()) {
		case MouseEvent.MOUSE_ENTERED:
		case MouseEvent.MOUSE_EXITED:
		case MouseEvent.MOUSE_PRESSED:	
		case MouseEvent.MOUSE_DRAGGED:
		case MouseEvent.MOUSE_RELEASED:
			requestFocus();
			status.setSelected(true);
			break;
			
		case KeyEvent.KEY_PRESSED:
			KeyEvent ke = (KeyEvent) e;
			int code = ke.getKeyCode();
			logger.debug("Key pressed : " + code);
			if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_A) {
				wheel.left();
			} else if (code == KeyEvent.VK_UP || code == KeyEvent.VK_W) {
				wheel.front();
			} else if (code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_D) {
				wheel.right();
			} else if (code == KeyEvent.VK_Z || code == KeyEvent.VK_X) {
				wheel.back();
			} else if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_S) {
				wheel.stop();
			} else if (code == KeyEvent.VK_NUMPAD8) {
				head.up();
			} else if (code == KeyEvent.VK_NUMPAD4) {
				head.left();
			} else if (code == KeyEvent.VK_NUMPAD6) {
				head.right();
			} else if (code == KeyEvent.VK_NUMPAD2) {
				head.down();
			} else if (code == KeyEvent.VK_NUMPAD5) {
				head.center();
			} else if (code == KeyEvent.VK_ESCAPE || code == KeyEvent.VK_ENTER) {
				head.center();
				wheel.stop();
			}
			updatePosition();
			break;
		case FocusEvent.FOCUS_GAINED:
			status.setSelected(true);
			break;
		case FocusEvent.FOCUS_LOST:
			status.setSelected(false);
			break;
		}
	}
	
	private void updatePosition() {
		robotControllerProxy.setPosition(head.getValueX(), head.getValueY(), wheel.getValueX(), wheel.getValueY());
	}

	protected boolean isFocusable() {
		return true;
	}
	

	private static class Block extends Widget {

		private static final Layout layout = new InlineLayout(2, 5, 2, 5, 4);
		{ setLayout(layout); }
		
		Text textBox;

		private Block(String text) {
			textBox = new Text(text);
			add(textBox);
		}

		protected void paint(Graphics2D g) {
			g.setColor(Color.lightGray);
			g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
			g.setColor(new Color(0xe0e0e0));
			g.fillRect(1, 1, getWidth() - 2, getHeight() - 2);
			g.setColor(Color.black);
		}

		protected boolean isFocusable() {
			return false;
		}

		protected Widget getWidgetAt(int mx, int my) {
			return null;
		}
	}
	
	private static class AutoCheckBox extends CheckBox {
		
		private AutoCheckBox(String text) {
			super(text);
		}
		
		public boolean isFocusable() {
			return false;
		}
	}
	
	public static void main(String[] args) {
		ControlPane controlPane = new ControlPane(null);
		controlPane.setBounds(0, 0, 101, 250);
		JFrame frame = new RootPane(controlPane).showFrame(101, 255);
		frame.setResizable(false);
		frame.setTitle("AoS - Arduino over Skype");
		
		logger.info("Client initialized.");
	}
}
